import 'dart:ui';

import 'package:flutterclue/main.dart';

import 'AndroidPopWindow.dart';
import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';

///首页筛选栏--体型筛选按钮
class PopupTypeButton extends StatefulWidget {
  const PopupTypeButton({this.originData, this.onSelect});

  final ValueChanged<List<String>> onSelect;
  final List<String> originData;

  @override
  State<StatefulWidget> createState() => new _PopupTypeButtonState();
}

class _PopupTypeButtonState extends State<PopupTypeButton> {
  List<String> _filterData;

  _onSelect(List<String> filterData) {
    setState(() {
      _filterData = filterData;
    });
    if (widget.onSelect != null) widget.onSelect(filterData);
  }

  @override
  Widget build(BuildContext context) {
//定义一个按钮，添加PopupTypeWindow为关联弹窗界面。
    return new PopupButton<List<String>>(
        name: "全部",
        onSelected: (value) => _onSelect(value),
        child: new PopupTypeWindow(
          originData: widget.originData,
          selectData: _filterData,
        ),
        fullWidth: true,
        offset: new Offset(0.0, 0.0));
  }
}

///首页体型筛选弹窗
class PopupTypeWindow extends StatefulWidget {
  const PopupTypeWindow({this.originData, this.selectData});

  final List<String> originData;
  final List<String> selectData;

  @override
  State<StatefulWidget> createState() => new _PopupTypeWindowState();
}

class _PopupTypeWindowState extends State<PopupTypeWindow> with SingleTickerProviderStateMixin{
  final List<String> _originData = [];
  final int _columnNum = 3; //每行个数
  AnimationController _controller;
  Animation<double> _height;
  Animation<double> _opacity;

  _reset() {
    setState(() {
      _originData.forEach((data) => data = "");
    });
  }

  _confirm() {
    List<String> results = [];
    _originData.forEach((data) {
      if (data.isNotEmpty) results.add(data);
    });
    Navigator.of(context).pop(results);
  }

  _onChanged(String data) {
    for (int i = 0; i < _originData.length; i++) {
      if (data == _originData[i]) {
        _originData[i] = data;
        break;
      }
    }
  }

  bool _isActive(int id) {
    if (widget.selectData != null) {
      for (int i = 0; i < widget.selectData.length; i++) {
        if (id.toString() == widget.selectData[i]) {
          return true;
        }
      }
    }
    return false;
  }

  _initData() {
    int length = widget.originData == null ? 0 : widget.originData.length;
    for (int i = 0; i < length; i++) {
      String data = widget.originData[i];
      data = "${i}";
      _originData.add(data);
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();

  }

  @override
  void initState() {
    super.initState();
    _initData();
    _controller =
        AnimationController(vsync: this, duration: Duration(milliseconds: 300));
    _height = Tween<double>(begin: 0, end: 500)
        .animate(CurvedAnimation(parent: _controller, curve: Curves.decelerate))
          ..addListener(() {
            setState(() {});
          });

    _opacity = Tween<double>(begin: 0.0, end: 1.0)
        .animate(CurvedAnimation(parent: _controller, curve: Curves.decelerate))
          ..addListener(() {
            setState(() {});
          });

    _controller.forward();
  }

  @override
  void didUpdateWidget(PopupTypeWindow oldWidget) {
    super.didUpdateWidget(oldWidget);
    _initData();
  }

  @override
  Widget build(BuildContext context) {
    print(
        "屏幕高度${MediaQuery.of(context).size.height}--状态栏高度${ScopedModel.of<CounterModel>(context).appBarH}");
//    _initData();
//
    List<Widget> child = [];
    Row row;
    for (int i = 0; i < _originData.length; i++) {
      if (i % _columnNum == 0) {
        row = new Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[],
        );
        child.add(Container(
          color: Colors.white,
          child: new Padding(
            padding: const EdgeInsets.only(top: 15.0),
            child: row,
          ),
        ));
      }
      row.children.add(new PopupItem(
        data: _originData[i],
        onChanged: (String data) => _onChanged(data),
      ));
    }
    child.add(Container(
      color: Colors.white,
      child: new Padding(
          padding: const EdgeInsets.only(top: 15.0),
          child: new Column(children: <Widget>[
            new Divider(height: 0.0),
            new Row(children: <Widget>[
              new Expanded(
                  child: new FlatButton(
                      onPressed: _reset,
                      child: const Text("dsfsdf"),
                      color: Theme.of(context).bottomAppBarColor)),
              new Expanded(
                  child: new FlatButton(
                      onPressed: _confirm,
                      child: const Text(
                        "sfsd",
                        style: const TextStyle(color: Colors.white),
                      ),
                      color: Theme.of(context).primaryColor)),
            ])
          ])),
    ));

    return ScopedModelDescendant<CounterModel>(
      builder: (context, wighet, mode) {
        return Opacity(
          opacity: _opacity.value,
          child: Container(
            child: Column(
              children: <Widget>[
                Container(height: _height.value, child: Column(children: child)),
                Expanded(
                  child: GestureDetector(
                    onTap: () {
                      Navigator.of(context).pop();
                    },
                  ),
                ),
              ],
            ),
            color: Color(0x55333333),
            height: (MediaQuery.of(context).size.height -
                MediaQueryData.fromWindow(window).padding.top -
                52),
          ),
        );
      },
    );
//      ScopedModelDescendant<CounterModel>(
//      builder: (context, chil1d, model) {
//        return
//      },
  }
}

///item控件
class PopupItem extends StatefulWidget {
  const PopupItem({this.data, this.onChanged});

  final String data;
  final ValueChanged<String> onChanged;

  @override
  State<StatefulWidget> createState() => new _PopupItemState();
}

class _PopupItemState extends State<PopupItem> {
  _onPress() {
    setState(() {});
    if (widget.onChanged != null) widget.onChanged(widget.data);
  }

  @override
  Widget build(BuildContext context) {
    return new GestureDetector(
      onTap: _onPress,
      child: new Column(
        children: <Widget>[
          new DecoratedBox(
            decoration: new BoxDecoration(
              color:
                  widget.data.isEmpty ? Theme.of(context).primaryColor : null,
              borderRadius: new BorderRadius.circular(50.0),
            ),
            child: new Padding(
              padding: const EdgeInsets.all(2.0),
              child: new Icon(Icons.arrow_drop_down),
            ),
          ),
          new Text(
            widget.data,
            style: new TextStyle(
                color: widget.data.isEmpty
                    ? Theme.of(context).primaryColor
                    : null),
          ),
        ],
      ),
    );
  }
}
